<template>
	<view class="container">
		<uni-forms err-show-type="undertext" :value="formData" ref="form" border="true" label-width="50">
				<uni-forms-item required label="内容" name="content" >
					<uni-easyinput type="textarea" autoHeight="true" trim="all" v-model="formData.content" placeholder="感谢您对Rok机器人的支持，任何问题我们将第一时间处理！" />
				</uni-forms-item>
			<button @click="submitForm('form')">提交</button>
		</uni-forms>
		<view>
			<!-- 提示信息弹窗 -->
			<uni-popup ref="message" type="message" :value="popupMessage" @change="popupChange">
				<uni-popup-message :type="popupMessage.msgType" :message="popupMessage.messageText" :duration="1000">
				</uni-popup-message>
			</uni-popup>
		</view>
	</view>

</template>

<script>
	export default {
		data() {
			return {
				formData: {
					content: ''
				},
				popupMessage: {
					type: 'center',
					msgType: 'success',
					messageText: '这是一条成功提示',
					value: ''
				},
				rules: {
					content: {
						rules: [{
								required: true,
								errorMessage: '请输入内容',
							},
							{
								minLength: 2,
								maxLength: 200,
								errorMessage: '输入内容长度在 {minLength} 到 {maxLength} 个字符',
							},
						]
					}
				}
			}
		},
		onReady() {
			this.$refs.form.setRules(this.rules)
		},
		methods: {
			submitForm: function(e) {
				let that = this
				uni.showLoading()
				this.$refs.form.submit().then(add => {
					add.userId= getApp().globalData.user.id
					uni.request({
						url: getApp().globalData.url + '/api/message/addPropose',
						method: "POST",
						header: {
							'content-type': 'application/json',
              'token':getApp().globalData.token
						},
						data: add,
						success: (res) => {
							if (res.data.code == "00000") {
								that.popupMessage.msgType = "success"
								that.popupMessage.messageText = "成功"
								that.$refs.message.open()
								
							} else {
								that.popupMessage.msgType = "error"
								that.popupMessage.messageText = res.data.msg
								that.$refs.message.open()
							}
						}
					});
					uni.hideLoading()
					console.log('表单数据信息：', add);
				}).catch(err => {
					uni.hideLoading()
					console.log('表单错误信息：', err);
				})
				
			},
			formReset: function(e) {
				console.log('清空数据')
			},
			popupChange:function(e){
				if(e.show==false){
					uni.redirectTo({
						url: "/pages/menu/menu"
					})
				}
			}
		},
		onShareAppMessage: function( options ){
		　　var that = this;
		　　var shareObj = {
		　　　　path: '/pages/menu/menu'
		　　};
		　　return shareObj;
		}
	}
</script>

<style>
	.uni-form-item .title {
		padding: 20rpx 0;
	}

	.container {
		padding: 30rpx;
	}
</style>
